package com.digiwin.dap.middleware.auth;

import org.springframework.util.Assert;

import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * 存储应用授权相关的数据
 */
public class AppAuthContext implements Serializable {
    /**
     * 登录成功用户
     */
    private AuthoredUser authoredUser;
    /**
     * 登录的sys
     */
    private AuthoredSys authoredSys;

    public AppAuthContext(){

    }
    /**
     * 构造函数
     * @param authoredUser
     * @param authoredSys
     */
   public AppAuthContext(@NotNull AuthoredUser authoredUser, AuthoredSys authoredSys){
       Assert.notNull(authoredUser,"authoredUser不能为null");
       this.authoredUser=authoredUser;
       this.authoredSys=authoredSys;
    }

    public AuthoredUser getAuthoredUser() {
        return authoredUser;
    }

    public void setAuthoredUser(AuthoredUser authoredUser) {
        this.authoredUser = authoredUser;
    }

    public AuthoredSys getAuthoredSys() {
        return authoredSys;
    }

    public void setAuthoredSys(AuthoredSys authoredSys) {
        this.authoredSys = authoredSys;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof AppAuthContext) {
            AppAuthContext test = (AppAuthContext)obj;
            if (this.getAuthoredUser() == null && test.getAuthoredUser() == null &&
            this.getAuthoredSys() == null && test.getAuthoredSys() == null
             ) {
                return true;
            }


            if (this.getAuthoredUser() != null && test.getAuthoredUser() != null && this.getAuthoredUser().equals(test.getAuthoredUser())
            && this.getAuthoredSys() != null && test.getAuthoredSys() != null && this.getAuthoredSys().equals(test.getAuthoredSys())
            ) {
                return true;
            }
        }

        return false;
    }

    @Override
    public int hashCode() {
        int hash= this.authoredUser == null ? -1 : this.authoredUser.hashCode();
        int hash2= this.authoredSys == null ? -1 : this.authoredSys.hashCode();
        return hash ^ hash2;
    }

}
